/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package UI;

import Logica.Accion;
import Persistencia.ADOAccion;
import Reporte.Reportes;
import java.util.Iterator;
import java.util.LinkedList;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author marco
 */
public class IAcciones extends javax.swing.JFrame {
    
    /**
     * Creates new form IAcciones
     */
    public IAcciones() {
        initComponents();
        this.cargarTabla(1);
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jMenu2 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jScrollPane1 = new javax.swing.JScrollPane();
        grdAcciones = new javax.swing.JTable();
        lblOrdenar = new javax.swing.JLabel();
        cmbOrdenar = new javax.swing.JComboBox();
        btnActualizarAccion = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        mnuAcciones = new javax.swing.JMenu();
        mnuAccionesNueva = new javax.swing.JMenuItem();
        mnuRepores = new javax.swing.JMenu();
        jMenuItem3 = new javax.swing.JMenuItem();

        jMenu2.setText("jMenu2");

        jMenuItem1.setText("jMenuItem1");

        jMenuItem2.setText("jMenuItem2");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowActivated(java.awt.event.WindowEvent evt) {
                formWindowActivated(evt);
            }
        });

        grdAcciones.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        grdAcciones.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID", "Nombre", "Descripcion", "Usuario", "Vencimiento", "Estado"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(grdAcciones);

        lblOrdenar.setText("Ordenar por:");

        cmbOrdenar.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "ID", "USUARIO", "VENCIMIENTO", " " }));
        cmbOrdenar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbOrdenarActionPerformed(evt);
            }
        });
        cmbOrdenar.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                cmbOrdenarPropertyChange(evt);
            }
        });

        btnActualizarAccion.setText("Actualizar Estado/Añadir Comentario");
        btnActualizarAccion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnActualizarAccionActionPerformed(evt);
            }
        });

        jMenu1.setText("File");
        jMenuBar1.add(jMenu1);

        mnuAcciones.setText("Acciones");

        mnuAccionesNueva.setText("Agregar Accion");
        mnuAccionesNueva.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuAccionesNuevaActionPerformed(evt);
            }
        });
        mnuAcciones.add(mnuAccionesNueva);

        jMenuBar1.add(mnuAcciones);

        mnuRepores.setText("Reportes");

        jMenuItem3.setText("Tareas Vencidas");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        mnuRepores.add(jMenuItem3);

        jMenuBar1.add(mnuRepores);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(lblOrdenar)
                .addGap(3, 3, 3)
                .addComponent(cmbOrdenar, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnActualizarAccion)
                .addGap(0, 55, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblOrdenar)
                    .addComponent(cmbOrdenar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnActualizarAccion))
                .addContainerGap(86, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void mnuAccionesNuevaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuAccionesNuevaActionPerformed
        ICargaAccion ica = new ICargaAccion();
        ica.setVisible(true);
        ica.toFront();
    }//GEN-LAST:event_mnuAccionesNuevaActionPerformed

    private void btnActualizarAccionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnActualizarAccionActionPerformed
        //Metodo el cual tras seleccionar una accion, abre una pantalla para actualizar su estado o añadirle un comentario
       /* IActualizarAccion iaa=new IActualizarAccion();
        if(grdAcciones.getSelectedRowCount()==1)
        {
            iaa.setCodAccion(Integer.parseInt(grdAcciones.getValueAt(grdAcciones.getSelectedRow(), 0).toString()));
            iaa.setNombreAccion(grdAcciones.getValueAt(grdAcciones.getSelectedRow(), 1).toString());
            
            switch (grdAcciones.getValueAt(grdAcciones.getSelectedRow(), 5).toString())
            {
                case("Abierta") : iaa.setEstado(1);
                break;
                case("Vencida") : iaa.setEstado(2);
                break;
                case("Cerrada") : iaa.setEstado(0);
                break;
            }            
            //iaa.setEstado(Integer.parseInt(grdAcciones.getValueAt(grdAcciones.getSelectedRow(), 5).toString()));
            iaa.setVisible(true);            
        }
        else
            JOptionPane.showMessageDialog(null, "Debe seleccionar solo una accion.");    
            **/
    }//GEN-LAST:event_btnActualizarAccionActionPerformed
   
    private void cmbOrdenarPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_cmbOrdenarPropertyChange
     
    }//GEN-LAST:event_cmbOrdenarPropertyChange

    private void cmbOrdenarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbOrdenarActionPerformed
       
        
        this.cargarTabla(this.cmbOrdenar.getSelectedIndex());
        
    
       
       
       
    }//GEN-LAST:event_cmbOrdenarActionPerformed

    private void formWindowActivated(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowActivated
        this.cargarTabla(0);
    }//GEN-LAST:event_formWindowActivated

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
        Reportes repo = new Reportes();
        ADOAccion a = new ADOAccion();
        repo.mostrarReporteTareasVencidas(a.traerAccionesParaReporte(0));
    }//GEN-LAST:event_jMenuItem3ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(IAcciones.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(IAcciones.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(IAcciones.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(IAcciones.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new IAcciones().setVisible(true);
            }
        });
    }
    
    
     public void cargarTabla(int orden){
     
          ADOAccion a = new ADOAccion();

          LinkedList acciones = a.traerAcciones(orden);
          Iterator iter = acciones.iterator();

        
                 
                 
         ((DefaultTableModel)IAcciones.this.grdAcciones.getModel()).setRowCount(0);

            while(iter.hasNext()){
                      Accion accionActual = (Accion)iter.next();
                      String estadoTexto = "";
                      switch (accionActual.getEstado()){
                      case(1) : estadoTexto = "Abierta";
                                 break;
                      case(2) : estadoTexto = "Vencida";
                                 break;
                      case(0) : estadoTexto = "Cerrada";
                                 break;
                      }
                     
                          String [] o = {""+accionActual.getId(),""+accionActual.getNombre(),""+accionActual.getDescripcion(),""+accionActual.getUsuario(),""+ accionActual.getFechaVencimientoStringInvertida(),""+estadoTexto};
                       ((DefaultTableModel)IAcciones.this.grdAcciones.getModel()).addRow(o);
                }
        
       

    }
     /*este metodo valida que no ingrese una fecha como 34 oun mes 13*/
     

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnActualizarAccion;
    private javax.swing.JComboBox cmbOrdenar;
    private javax.swing.JTable grdAcciones;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblOrdenar;
    private javax.swing.JMenu mnuAcciones;
    private javax.swing.JMenuItem mnuAccionesNueva;
    private javax.swing.JMenu mnuRepores;
    // End of variables declaration//GEN-END:variables
}
